package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddTag;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddTagService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_tag")
public class YddTagController {

    @Autowired
    private IYddTagService iYddTagService;

    @Autowired
    private IYddArticleService iYddArticleService;

    @PostMapping("add")
    public ResultData add(@RequestBody YddTag tag){
        Boolean flag = getaBoolean(tag);
        if (flag){
            return ResultData.error("标签已存在");
        }
        iYddTagService.save(tag);
        return ResultData.ok("添加成功","");
    }

    private Boolean getaBoolean(YddTag tag) {
        List<YddTag> list = iYddTagService.list();
        Boolean flag = false;
        for (YddTag yddTag : list) {
            if (tag.getTagname().equals(yddTag.getTagname())) {
                flag = true;
            }
        }
        return flag;
    }

    //回显数据
    @GetMapping("rowInfo")
    public ResultData rowInfo(int id){
        YddTag byId = iYddTagService.getById(id);
        return ResultData.ok("",byId);
    }


    @GetMapping("del")
    public ResultData del(int id){
        //根据id获取标签名
        YddTag tag = iYddTagService.getById(id);
        List<YddArticle> list = getYddArticles(tag);

        if (!CollectionUtils.isEmpty(list)){
            return ResultData.error("该标签与文章关联");
        }

        iYddTagService.removeById(id);
        return ResultData.ok("删除成功","");
    }

    //从文章的表中根据标签名查找数据
    private List<YddArticle> getYddArticles(YddTag tag) {
        String tagname = tag.getTagname();

        //使用模糊查询，判断是否有文章关联标签
        LambdaQueryWrapper<YddArticle> lqw =new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(tagname),YddArticle::getTags,tagname);
        List<YddArticle> list = iYddArticleService.list(lqw);
        return list;
    }

    @PostMapping("update")
    private ResultData update(@RequestBody YddTag tag){
        Boolean flag = getaBoolean(tag);
        if (flag){
            return ResultData.error("标签已存在");
        }
        //获取原始表签名
        YddTag byId = iYddTagService.getById(tag.getId());
        String tagname = byId.getTagname();

        //更新标签表数据
        iYddTagService.updateById(tag);

        //获取原始文章表的标签
        List<YddArticle> articleList = getYddArticles(byId);
        for (YddArticle article : articleList) {
            String tags = article.getTags();
            //更新标签
            String replace = tags.replace(tagname, tag.getTagname());
            article.setTags(replace);
        }
        //更新标签表数据
        iYddArticleService.updateBatchById(articleList);

        return ResultData.ok("更新成功","");
    }

    @GetMapping("list")
    public ResultData list(String tagname ,int page ,int limit){
        //构建分页
        Page<YddTag> tagPage = new Page<>(page,limit);
        //构建查询器
        LambdaQueryWrapper<YddTag> lqw =new LambdaQueryWrapper();
        lqw.eq(StringUtils.isNotEmpty(tagname),YddTag::getTagname,tagname);

        iYddTagService.page(tagPage,lqw);

        Map<String,Object> map =new HashMap<>();
        map.put("count",tagPage.getTotal());
        map.put("totalPages",tagPage.getPages());
        map.put("pageSize",tagPage.getPages());
        map.put("currentPage",tagPage.getCurrent());
        map.put("data",tagPage.getRecords());

        return ResultData.ok("",map);

    }

    //获取所有标签
    @GetMapping("all")
    public ResultData all(){
        List<YddTag> list = iYddTagService.list();
        return ResultData.ok("",list);
    }
}
